package com.bdqn.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.common.utils.PageUtils;
import com.bdqn.common.utils.Query;
import com.bdqn.gulimall.order.dao.OrderItemDao;
import com.bdqn.gulimall.order.entity.OrderEntity;
import com.bdqn.gulimall.order.entity.OrderItemEntity;
import com.bdqn.gulimall.order.entity.OrderReturnReasonEntity;
import com.bdqn.gulimall.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.stereotype.Service;

import java.util.Map;

// @RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>());

        return new PageUtils(page);
    }

    /**
     * queues:声明要监听的所有队列
     * <p>
     * 接收到的类型：org.springframework.amqp.core.Message
     * <p>
     * 参数可以写以下类型
     * 1.Message message：原生消息详细信息，头+体
     * 2.T<发送的消息类型> OrderReturnReasonEntity content
     * <p>
     * Channel channel:当前传输数据的通道
     * <p>
     * Queue：可以很多人来监听，只要收到消息，队列就会删除消息，且只能有一个人收到消息
     * 场景：
     * 1. 订单服务启动多个，同一个消息，只有一个客户端收到
     * 2.只有一个消息完全处理完，方法运行结束，就可以接收到下一个消息
     */
    // @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content,
                               //抛出sleep休眠方法的异常
                               Channel channel) throws InterruptedException {    //amqp核心包中的Message

        System.out.println("接收到消息……内容：" + content);
        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();//消息头的属性信息
        // Thread.sleep(3000);
        System.out.println("消息处理完成=>" + content.getName());
        //channel内按顺序会自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag==>" + deliveryTag);

        //签收货物，非批量模式
        try {
            if (deliveryTag % 2 != 0) {
                // 收货
                channel.basicAck(deliveryTag, false);
                System.out.println("已签收" + deliveryTag);
            } else {
                // 退货 requeue=false 丢弃 requeue=true 发回服务器，在服务器中重新入队
                // long deliveryTag, boolean multiple（是否批量拒绝）, boolean requeue(队列重新入队
                channel.basicNack(deliveryTag,false,true);
                // long deliveryTag, boolean requeue
                // channel.basicReject();
                System.out.println("未签收");
            }
        } catch (Exception e) {
            //网络中断
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMessage2(OrderEntity content) {    //amqp核心包中的Message
        System.out.println("接收到消息……内容：" + content);


    }

}